# Contributor: Robert T. McGibbon <rmcgibbo@gmail.com>

_realname=netcdf
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.6.1
pkgrel=1
pkgdesc="Interface for scientific data access to large binary data (mingw-w64)"
arch=('any')
license=('custom')
url="https://www.unidata.ucar.edu/software/netcdf/"
depends=("${MINGW_PACKAGE_PREFIX}-hdf5")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
source=("ftp://ftp.unidata.ucar.edu/pub/netcdf/${_realname}-${pkgver}.zip"
        0001-mingw-cmake.patch
        0002-dutil-correcttypo.patch
        0003-ncconfigure-strlcat-export.patch
        0004-ddispatch-set-temp.patch
        0005-dapcvt-conversion.patch
        0006-cvt-conversion.patch
        0007-mingw-config.patch)
sha256sums=('94ff06be0d5b998f6f5e31ed8fdbdeac50e6f8267fe9624a1a3ef2e34d0a45e4'
            '6990366e780dfba6f1070d6bd3eae66d79ff41f5d133c51d2409e3d071527407'
            'da666e1f762b3ecd2becc9ad55fe1e32be9a47b5bae4f7990e7d94aae69697cd'
            'a1047a8feb86732e2f6087db5240da955045f5f653f9406103ce733c4850b536'
            'aeb61f0a4d69e26b2235b15948ac6b99ac92be5b0f8155bfa7aa4867af84efa8'
            '4b7e818c06ef33fbbfc6fa12f43ada74f0d38c89e989141adadb22f9c3993810'
            '66f2b494142d4c39096d80765df38254bb3c46829636fccc4d085f63e65bdb6c'
            'a1aa112779a83ec74acd7de8a0a57915f5d37cae44633d0a4d0d20784a2d288c')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/0001-mingw-cmake.patch
  patch -p1 -i ${srcdir}/0002-dutil-correcttypo.patch
  patch -p1 -i ${srcdir}/0003-ncconfigure-strlcat-export.patch
  patch -p1 -i ${srcdir}/0004-ddispatch-set-temp.patch
  patch -p1 -i ${srcdir}/0005-dapcvt-conversion.patch
  patch -p1 -i ${srcdir}/0006-cvt-conversion.patch
  patch -p1 -i ${srcdir}/0007-mingw-config.patch
}

build() {
  [[ -d ${srcdir}/build-static-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-static-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-static-${MINGW_CHOST} && cd ${srcdir}/build-static-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=OFF \
    -DENABLE_DLL=OFF \
    -DENABLE_DAP=ON \
    -DENABLE_TESTS=OFF \
    -DENABLE_DYNAMIC_LOADING=OFF \
    -DENABLE_NETCDF_4=ON \
    "${srcdir}/${_realname}-${pkgver}"
  make

  [[ -d ${srcdir}/build-shared-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-shared-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-shared-${MINGW_CHOST} && cd ${srcdir}/build-shared-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=ON \
    -DENABLE_DAP=ON \
    -DENABLE_TESTS=OFF \
    -DENABLE_DYNAMIC_LOADING=OFF \
    -DENABLE_NETCDF_4=ON \
    -DENABLE_LOGGING=ON \
    "${srcdir}/${_realname}-${pkgver}"
  make
}

package() {
  cd ${srcdir}/build-static-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install

  cd ${srcdir}/build-shared-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYRIGHT \
    ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYRIGHT

  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  find ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig -name *.pc -exec sed -i -e"s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" {} \;
}
